/*
 * Created on Feb 13, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.engine;

import java.util.ArrayList;
import java.util.List;

import edu.mit.simile.longwell.query.IPage;
import edu.mit.simile.longwell.query.Query;

/**
 * @author dfhuynh
 */
public class Page implements IPage {
	final protected List 	m_allObjects;
	
	final protected int		m_pageNumber;
	final protected int		m_startIndex;
	final protected int		m_count;
	
	final protected Query	m_query;
	
	protected List m_objects;
	
	public Page(
			List	allObjects,
			int 	pageNumber, 
			int 	startIndex, 
			int 	count, 
			Query 	query
		) {
		m_allObjects = allObjects;
		
		m_pageNumber = pageNumber;
		m_startIndex = startIndex;
		m_count = count;
		
		m_query = query;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getPageNumber()
	 */
	public int getPageNumber() {
		return m_pageNumber;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getStartIndex()
	 */
	public int getStartIndex() {
		return m_startIndex + 1;
	}
	
	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getEndIndex()
	 */
	public int getEndIndex() {
		return getStartIndex() + getCount() - 1;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getCount()
	 */
	public int getCount() {
		return m_count;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getObjects()
	 */
	public List getObjects() {
		if (m_objects == null) {
			m_objects = new ArrayList(
				m_allObjects.subList(m_startIndex, m_startIndex + m_count));
		}
		return m_objects;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.IPage#getQuery()
	 */
	public Query getQuery() {
		return m_query;
	}
}
